package com.kcs.fxmargin.export.excel;

import java.math.BigDecimal;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HeaderFooter;
import org.apache.poi.hssf.util.CellRangeAddress;

import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.export.model.ReportCriteria;


public class BaseReportExcel {

   protected void createHeaderPage(HSSFWorkbook wb, HSSFSheet sheet, ReportCriteria criteria) throws Exception{
        /**
         * Create Header 
         */
	    HeaderFooter header = sheet.getHeader();

	    if(commonUtils.isNotEmpty(criteria.getLeftHeader())){
	    	header.setLeft(""+criteria.getLeftHeader());      
	    }
	    if(commonUtils.isNotEmpty(criteria.getCenterHeader())){
	    	header.setCenter(""+criteria.getCenterHeader());
	    }
	    if(commonUtils.isNotEmpty(criteria.getRightHeader())){
	    	header.setRight(""+criteria.getRightHeader());
	    }
	    
    }

    protected void createFooterPage(HSSFWorkbook wb, HSSFSheet sheet, ReportCriteria criteria) {

        String printBy   = "Print By : " + criteria.getLeftFooter();
        String printDate = ""+commonUtils.getcurrentDatetime();

        HSSFFooter footer = sheet.getFooter();
        footer.setLeft(printBy);
        footer.setRight(printDate);
    }
    
    protected void mergeCell(HSSFSheet sheet, HSSFRow row, String value, int startIndex, int endIndex, HSSFCellStyle style){
        int i = startIndex;
        HSSFCell cell = row.createCell(i++);
        cell.setCellValue(value);
        cell.setCellStyle(style);
        
        for(;i<=endIndex;i++){
            cell = row.createCell(i);
            cell.setCellStyle(style);
        }
        sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), startIndex, endIndex));
    }
    
    protected HSSFRow createNewRow(HSSFSheet sheet){
        return sheet.createRow(sheet.getRow(0)!=null?sheet.getLastRowNum()+1:0);
    }
    
    protected HSSFCell createNewCell(HSSFRow  row){
        return row.createCell(row.getLastCellNum()>0?row.getLastCellNum():0);
    }
    
    protected HSSFCell createNewCell(HSSFRow  row, String value){
        HSSFCell cell = createNewCell(row);
        cell.setCellValue(value!=null?value:"");
        return cell;
    }
    
    protected HSSFCell createNewCell(HSSFRow row, String value, HSSFCellStyle cellStyle){
    	HSSFCell cell = createNewCell(row);
        cell.setCellValue(value!=null?value:"");
        cell.setCellStyle(cellStyle);
        return cell;
    }
    
    protected HSSFCell createNewCell(HSSFRow row, BigDecimal value, HSSFCellStyle cellStyle){
        Double d = null;
        if(value!=null){
            d = value.doubleValue();
        }
        return createNewCell(row, d, cellStyle);
    }
    
    protected HSSFCell createNewCell(HSSFRow row, Double value, HSSFCellStyle cellStyle){
        String valueStr = "";
        if(value!=null && value.doubleValue()!=0){
            if(value.doubleValue()>0){
                valueStr = commonUtils.toCurrencyFormat(value.doubleValue());
            }else{
                valueStr = "(" + commonUtils.toCurrencyFormat(Math.abs(value)) + ")";
            }
        }
        return createNewCell(row,valueStr,cellStyle);
    }
    
    protected HSSFCell createNewCell(HSSFRow row, double value, HSSFCellStyle cellStyle){
        return createNewCell(row,value,cellStyle,true);
    }
    
    
    protected HSSFCell createNewCell(HSSFRow row, double value, HSSFCellStyle cellStyle, boolean isShowZero){
        String valueStr = "";
        if(value>=0){
            if(isShowZero || value > 0){
                valueStr = commonUtils.toCurrencyFormat(value);
            }
        }else{
            valueStr = "(" + commonUtils.toCurrencyFormat(Math.abs(value)) + ")";
        }
        return createNewCell(row,valueStr,cellStyle);
    }

}
